
#include "stdafx.h"
#define MYLIB_API_EXPORT
#include "PesudoColorperTriangle.h"
#include <math.h>

using namespace std;

struct RGB_stru
{
	float f_r, f_g, f_b;
	RGB_stru()
	{
		f_r = 0.0;
		f_g = 0.0;
		f_b = 0.0;
	}
	RGB_stru(const float& r, const float& g,const float& b)
	{
		f_r = r;
		f_g = g;
		f_b = b;
	}
};

struct RGB_stru_255
{
	int i_r, i_g, i_b;
	RGB_stru_255()
	{
		i_r = 0;
		i_g = 0;
		i_b = 0;
	}
	RGB_stru_255(const int& r, const int& g,const int& b)
	{
		i_r = r;
		i_g = g;
		i_b = b;
	}
};

//The main function of this header
MYLIB_API void color_per_triangle(const ModelOBJ* Obj_model, const string& Output_path)
{
	long lNumber_of_triangles = 0;
	//read the obj file and count the number of triangles
	SOLID_const_itr s_itr = Obj_model->begin();
	while (s_itr != Obj_model->end())
	{
		SolidOBJ* solid = *s_itr++;
		lNumber_of_triangles += solid->Solid_mesh->size_of_facets();
	}
	//create a color list for each triangle
	vector<RGB_stru> color_list;
	color_list.reserve(lNumber_of_triangles);

	long lNumber_of_colors = pow(lNumber_of_triangles,1.0/3.0);
	float fstep = 1.0 / (lNumber_of_colors + 0.5);//the color step for triangles

	float fcolor[3] = {0.0, 0.0, 0.0};//r g b
	long lcurrent = 0;//current triangle

	while (fcolor[0] <= 1.0)//r
	{
		while (fcolor[1] <= 1.0)//g
		{
			while (fcolor[2] <= 1.0)//b
			{
				if (lcurrent == lNumber_of_triangles)
				{
					goto FinishColor;
				}
				RGB_stru RGB_a_color(fcolor[0], fcolor[1], fcolor[2]);
				color_list.push_back(RGB_a_color);//[lcurrent] = RGB_a_color;
				lcurrent ++;

				fcolor[2] += fstep;
			}
			fcolor[2] = 0.0;
			fcolor[1] += fstep;
		}
		fcolor[1] = 0.0;
		fcolor[0] += fstep;
	}

FinishColor://color list has been made
	//output the color list to a file
	fstream myfile;
	myfile.open(Output_path, ios::out);
	if (!myfile.is_open())
	{
		cerr<<"Open error"<<endl;
		return;
	}
	//header
	myfile<<"The color list generated by Persim"<<endl;
	myfile<<lNumber_of_triangles<<endl;
	//body
	myfile.precision(6);
	vector<RGB_stru>::iterator color_itr = color_list.begin();
	while (color_itr != color_list.end())
	{
		myfile<<color_itr->f_r<<" "<<color_itr->f_g<<" "<<color_itr->f_b<<endl;
		++color_itr;
	}
	myfile.close();
}

//The main function of this header by 255
MYLIB_API void color_per_triangle_255(const ModelOBJ* Obj_model, const string& Output_path)
{
	long lNumber_of_triangles = 0;
	//read the obj file and count the number of triangles
	SOLID_const_itr s_itr = Obj_model->begin();
	while (s_itr != Obj_model->end())
	{
		SolidOBJ* solid = *s_itr++;
		lNumber_of_triangles += solid->Solid_mesh->size_of_facets();
	}
	//create a color list for each triangle
	vector<RGB_stru_255> color_list;
	color_list.reserve(lNumber_of_triangles);

	long lNumber_of_colors = pow(lNumber_of_triangles,1.0/3.0);
	int istep = (int)(255 / (lNumber_of_colors + 0.5));//the color step for triangles
	istep = (1 > istep)? 1 : istep;

	int icolor[3] = {0.0, 0.0, 0.0};//r g b
	long lcurrent = 0;//current triangle

	while (icolor[0] <= 255)//r
	{
		while (icolor[1] <= 255)//g
		{
			while (icolor[2] <= 255)//b
			{
				if (lcurrent == lNumber_of_triangles)
				{
					goto FinishColor;
				}
				RGB_stru_255 RGB_a_color(icolor[0], icolor[1], icolor[2]);
				color_list.push_back(RGB_a_color);//[lcurrent] = RGB_a_color;
				lcurrent ++;

				icolor[2] += istep;
			}
			icolor[2] = 0.0;
			icolor[1] += istep;
		}
		icolor[1] = 0.0;
		icolor[0] += istep;
	}

FinishColor://color list has been made
	//output the color list to a file
	fstream myfile;
	myfile.open(Output_path, ios::out);
	if (!myfile.is_open())
	{
		cerr<<"Open error"<<endl;
		return;
	}
	//header
	myfile<<"The color list generated by Persim"<<endl;
	myfile<<lNumber_of_triangles<<endl;
	//body
	myfile.precision(6);
	vector<RGB_stru_255>::iterator color_itr = color_list.begin();
	while (color_itr != color_list.end())
	{
		myfile<<color_itr->i_r<<" "<<color_itr->i_g<<" "<<color_itr->i_b<<endl;
		++color_itr;
	}
	myfile.close();
}